import simpy
import random

# 1. 随即数种子保证每次产生结果都一样
# 2. 医生看病耗时是指数分布 lambda=3
# 3. 模拟进入就诊队列的时间
# 4. 模拟看医生时间
# 5. 模拟离开医生时间

RANDOM_SEED = 42
random.seed(RANDOM_SEED)

class Outpatient:

    def __init__(self, env, id):
        self.env = env
        self.id = id
        self.name = f"P_{id}"

    def see_doctor(self, doctor):
        self.after_registry_time = env.now
        print(f'患者 {self.name} 挂号于 {self.after_registry_time}， 进入就诊队列...')
        with doctor.request() as req:
            yield req
            see_doctor_duration = random.expovariate(3)
            queue_time = env.now - self.after_registry_time
            print(f'患者: {self.name} 看医生于: {env.now}， 排队耗时: {queue_time}')
            yield env.timeout(see_doctor_duration)
            print(f'患者: {self.name} 离开医生于: {env.now}')

env = simpy.Environment()
doctor = simpy.Resource(env, capacity=1)

for i in range(0, 3):
    out_patient = Outpatient(env, i)
    env.process(out_patient.see_doctor(doctor))

env.run(until=20)
